﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Service;
using PersonalHealth.Data.Model;
using PersonalHealth.Data.Data;
using PersonalHealth.Data.IService;
using System.Data.Entity;
using Core.UnitOfWork;
using PersonalHealth.Data.Repository;

namespace PersonalHealth.Data.Service
{
    public class DoctorService : Service<Doctor, HealthContext>, IDoctorService
    {

        public Doctor GetDoctorDetail(int id)
        {
            using (DbContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);

                IDoctorRepository drep = new DoctorRepository(unitOfWork);

                return drep.GetDoctorDetail(id);
            }
        }

        public IList<Doctor> GetDoctorDetailList<Tkey>(Func<Doctor, bool> expression,
          Func<Doctor, Tkey>[] orderby, bool[] isDescending, int pageIndex, int pageCount, out int pages, out int records)
        {
            using (DbContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                unitOfWork.LazyLoadingEnabled = false;
                unitOfWork.ProxyCreationEnabled = false;

                IDoctorRepository drep = new DoctorRepository(unitOfWork);

                return drep.GetDoctorDetailList(expression, orderby, isDescending, pageIndex, pageCount,out pages,out records);
            }
        }

        public void SendHealthReport(int measureUserId,string title,string text)
        {
            using (DbContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                IHealthReportRepository drep = new HealthReportRepository(unitOfWork);

                HealthReport obj = new HealthReport();

                obj.UserId = measureUserId;
                obj.Title = title;
                obj.Text = text;
                obj.CreateDate = obj.ModifyDate = DateTime.Now;
                
                drep.Add(obj);
                unitOfWork.Save();
            }
        }

        public void SendMessage(int doctorId,int measureUserId, string title, string text)
        {
            using (DbContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                IMessageRepository drep = new MessageRepository(unitOfWork);

                Message obj = new Message();
                obj.FromUserId = doctorId;
                obj.ToUserId = measureUserId;
                obj.Title = title;
                obj.Msg = text;
                obj.MsgTypeId = (int)MessageType.Doctor;
                obj.CreateDate = obj.ModifyDate = DateTime.Now;
                obj.NotRead = true;

                drep.Add(obj);
                unitOfWork.Save();
            }
        }

        public void SendTask(int measureUserId, string title, string text)
        {
            using (DbContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                ITaskRepository drep = new TaskRepository(unitOfWork);

                Task obj = new Task();
                obj.UserId = measureUserId;
                obj.Title = title;
                obj.Text = text;
                obj.TaskTypeId = (int)TaskType.Goal;
                obj.CreateDate = obj.ModifyDate = DateTime.Now;

                drep.Add(obj);
                unitOfWork.Save();
            }
        }
    }
}
